package com.example.caxandroid;

import android.R.bool;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements SensorEventListener
{
	 MediaPlayer mp;
	 MediaPlayer mpTorcida;
	 float xAnterior,yAnterior ,zAnterior ;
	 boolean primeiraVolta = false;
	 boolean tocando = false ;
	 float RUIDO =  2.0f;
	 SeekBar seekbar;
	 TextView value;
	 
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        value =(TextView)findViewById(R.id.textView1);
        seekbar = (SeekBar)findViewById(R.id.seekBar1);
        
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				value.setText("Sensibilidade_Sensibility "+arg1);
				RUIDO = RUIDO * (arg1 / 100);
				
			}
		});
        mp = MediaPlayer.create(getApplicationContext(), R.raw.caxidroid2);
        mpTorcida = MediaPlayer.create(getApplicationContext(),R.raw.stadium);
        mpTorcida.setLooping(true);
    }

    void ligarTorcida(View view)
    {
    	boolean liga = ((ToggleButton) view).isChecked();
    	if (liga)
    	{
    		mpTorcida.start();
    	}
    	else
    	{
    		mpTorcida.stop();
    	}
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onSensorChanged(SensorEvent event) {
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		if (!primeiraVolta) {
		xAnterior = x;
		yAnterior = y;
		zAnterior = z;
		
		primeiraVolta = true;
		} else {
		float deltaX = Math.abs(xAnterior- x);
		float deltaY = Math.abs(yAnterior - y);
		float deltaZ = Math.abs(zAnterior - z);
		if (deltaX < RUIDO) deltaX = (float)0.0;
		if (deltaY < RUIDO) deltaY = (float)0.0;
		if (deltaZ < RUIDO) deltaZ = (float)0.0;
		xAnterior = x;
		yAnterior = y;
		zAnterior = z;
		
		if (deltaX > deltaY)
		{
			if(!tocando)
			{
				mp.start();
				tocando = true;
			}
		} 
		else if (deltaY > deltaX) 
		{
			if(!tocando)
			{
				mp.start();
				tocando = true;
			}
		} 
		else
		{
			mp.stop();
			tocando =false;
		}
		}
		
		
	}
}
